import { AssetManager, Canvas, Component, _decorator, assetManager, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Boost')
export class Boost extends Component {

    @property(Canvas)
    private canvas2d: Canvas = null;

    @property(Node)
    private toReleaseNode: Node = null;

    /**加载包 */
    private loadBundle(bundleName: string): Promise<AssetManager.Bundle> {
        return new Promise<AssetManager.Bundle>(resolve => {
            assetManager.loadBundle(bundleName, (e, asset) => {
                if (e) {
                    console.error(e);
                    resolve(null);
                    return;
                }
                resolve(asset);
            });
        })
    }

    async start() {
        /**加载全局脚本包 */
        await this.loadBundle("GScriptBN");
        const gCtrl: any = this.node.addComponent("GCtrl");
        await gCtrl.init({
            canvas2d: this.canvas2d,
            releaseBoostFun: () => {
                //这里销毁首场景渲染节点
                if (this.toReleaseNode == null) {
                    return;
                }
                this.toReleaseNode.destroy();
                this.toReleaseNode = null;
            }
        })
    }
}


